package com.woniuxy.staffservice.provider;

import com.woniuxy.staffservice.entity.Dept;
import org.apache.ibatis.jdbc.SQL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DeptProvider {
    private static final Logger logger = (Logger) LoggerFactory.getLogger(DeptProvider.class);

    public String select(Dept dept, Integer index, Integer limit) {
        return new SQL() {{
            if (dept.getId() == null) {
                SELECT("a.*,b.name as pname");
                FROM("sys_dept a,sys_dept b");
                WHERE("a.pid=b.id");
                OR();
                ORDER_BY("a.id");
            }
            if (dept.getId() != null) {
                SELECT("a.*,b.name as pname");
                FROM("sys_dept a");
                LEFT_OUTER_JOIN("sys_dept b on a.pid =" + dept.getId());
            }
            if (dept.getName() != null && dept.getName().length() != 0) {
                WHERE("a.name like '%${name}%'");
            }
            if (dept.getAddress() != null && dept.getAddress().length() != 0) {
                WHERE("a.address like '%${address}%'");
            }
            if (dept.getDescription() != null && dept.getDescription().length() != 0) {
                WHERE("a.description like '%${description}%'");
            }
            LIMIT(index + "," + limit);
        }}.toString();
    }
}
